package com.srp.servise.impl;


import com.srp.mapper.UserMapper;
import com.srp.pojo.CreateOrLoginReturn;
import com.srp.pojo.EditUserInfoReturn;
import com.srp.pojo.GetUserInfoReturn;
import com.srp.servise.UserService;
import com.srp.tools.AliOSSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired

     AliOSSUtils aliOSSUtils;

    private String uplaod(String id,MultipartFile image) throws IOException {
        String url;
        //上传文件到OSS
        url = aliOSSUtils.upload(id,image);
        return url;
    }
    public CreateOrLoginReturn createUser(String phone, String password) {
        if(phone==null||phone==""||password==null||password=="")
        {return new CreateOrLoginReturn(Boolean.FALSE,"");}
        else if (userMapper.getCountWherePasswordAndPhone(password, phone) == 0) {
            String id = UUID.randomUUID().toString();
            userMapper.insert(id, password, phone, null, null,LocalDateTime.now(), LocalDateTime.now());
            return new CreateOrLoginReturn(Boolean.TRUE, id);
        } else {
            return new CreateOrLoginReturn(Boolean.FALSE, userMapper.getIdWherePasswordAndPhone(password, phone));
        }




    }

    public CreateOrLoginReturn loginUser(String phone, String password) {
        if (userMapper.getCountWherePasswordAndPhone(password, phone) == 1) {
            return new CreateOrLoginReturn(Boolean.TRUE,userMapper.getIdWherePasswordAndPhone(password, phone) );
        } else {
            return new CreateOrLoginReturn(Boolean.FALSE, null);
        }
    }

    public EditUserInfoReturn editInformation(String id, MultipartFile avatar, String name) throws IOException {
        int state=0;
        String newName=null;
        String newImageUrl=null;
        if(id==""||id==null){
            state=0;
        }
        else if(userMapper.getCountWhereId(id)==0){
            state = 0;
        }
        else if((avatar==null||avatar.isEmpty())&&(name.length()==0||name==null)){
            state = 0;
            newName = userMapper.getNameWhereId(id);
            newImageUrl = userMapper.getURLWhereId(id);
        }
        else if(avatar!=null&&(name.length()==0||name==null)){
            state = 1;
            newName = userMapper.getNameWhereId(id);
            newImageUrl=uplaod(id,avatar);
            userMapper.updateURL(newImageUrl,id);
        }
        else if(name!=null&&avatar==null){
            state = 2;
            newName=name;
            newImageUrl = userMapper.getURLWhereId(id);
            userMapper.updateName(name,id);
        }
        else{
            state = 3;
            newName=name;
            userMapper.updateName(name,id);
            newImageUrl=uplaod(id,avatar);
            userMapper.updateURL(newImageUrl,id);
        }
        return new  EditUserInfoReturn(state,newName,newImageUrl);
    }

    public GetUserInfoReturn getUserInfo(String id){
        return userMapper.getUserInfoWherePasswordAndPhone(id);
    };



}
